/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007-2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.security.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.security.AccessDeniedException;
import org.springframework.security.AuthenticationException;

/**
 * filter to retrieve a buried spring security exception.
 */
public class PreExceptionTranslaterFilter
    implements Filter
{

  /**
   * Do nothing.
   * 
   * @see javax.servlet.Filter#destroy()
   */
  public void destroy()
  {
    // Do nothing.
  }



  /**
   * Translate eventual exception to retrieve the spring security exceptions.
   * 
   * @param request
   *          the http request
   * @param response
   *          the http response
   * @param chain
   *          the remaining filter chain
   * @throws IOException
   *           if the remaining filters throw an {@link IOException}
   * @throws ServletException
   *           if the remaining filters throw an {@link ServletException}
   * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
   *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
   */
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException
  {
    try {
      chain.doFilter(request, response);
    }
    catch (RuntimeException e) {
      translateException(e);
      throw e;
    }
    catch (ServletException e) {
      translateException(e.getRootCause());
      throw e;
    }
  }



  /**
   * check the given exception and throws an eventual spring security exception.
   * 
   * @param e
   *          the exception to check
   */
  private void translateException(Throwable e)
  {
    if (e instanceof AuthenticationException) {
      throw (AuthenticationException) e;
    }
    if (e instanceof AccessDeniedException) {
      throw (AccessDeniedException) e;
    }
    if (e instanceof ServletException) {
      translateException(((ServletException) e).getRootCause());
    }
    if (e != null && e.getCause() != e) {
      translateException(e.getCause());
    }
  }



  /**
   * Do nothing.
   * 
   * @param filterConfig
   *          the configuration of the filer
   * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
   */
  public void init(FilterConfig filterConfig)
  {
    // Do nothing.
  }

}
